package com.sls.web.manager.game.pojo.ekhui;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import com.sls.core.common.MD5Util;
/**账户
 * @param args
 */
public class Account implements Serializable {
	private static final long serialVersionUID = 3828388109978206150L;
	private String accountId ;
	private String accountName ;		//账户名称
	private String traderId ;			//账户所属商户id
	private String traderName ;			//账户所属商户名称
	private String payPassword ;		//密码
	private BigDecimal balance ;		//余额
	private BigDecimal freezeMoney ;	//冻结金额
	private BigDecimal availableMoney ;	//可用余额
	private String currency ;			//账户币种，rmb或者$
	private String accountState ;		//账户状态(0:待审核,1:正常,2:冻结)
	private Date lastModifyTime ;		//账户的上次修改时间
	private String createOperatorId ;		//账户创建操作员
	private Date createTime ;			//账户创建时间
	private String accountType ;		//账户类别,(1:保证金账户,2:佣金账户,3:红利账户,4:业务账号)
	private String accountMac ;			//mac校验字段,防止篡改
	private Date accountModifyTime ; 	//余额变动账务日期
	private BigDecimal lastBalance ;//上期资金余额
	private String remark ;			//备注
	private int version ;				//版本号，用于乐观锁
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getAccountId() {
		return accountId;
	}
	public void setAccountId(String accountId) {
		this.accountId = accountId;
	}
	public String getAccountName() {
		return accountName;
	}
	public BigDecimal getLastBalance() {
		return lastBalance;
	}
	public void setLastBalance(BigDecimal lastBalance) {
		this.lastBalance = lastBalance;
	}
	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}
	public String getTraderId() {
		return traderId;
	}
	public void setTraderId(String traderId) {
		this.traderId = traderId;
	}
	public String getPayPassword() {
		return payPassword;
	}
	public void setPayPassword(String payPassword) {
		this.payPassword = payPassword;
	}
	public BigDecimal getBalance() {
		return balance;
	}
	public void setBalance(BigDecimal balance) {
		this.balance = balance;
	}
	public BigDecimal getFreezeMoney() {
		return freezeMoney;
	}
	public void setFreezeMoney(BigDecimal freezeMoney) {
		this.freezeMoney = freezeMoney;
	}
	public BigDecimal getAvailableMoney() {
		return availableMoney;
	}
	public void setAvailableMoney(BigDecimal availableMoney) {
		this.availableMoney = availableMoney;
	}
	public Date getLastModifyTime() {
		return lastModifyTime;
	}
	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}
	public String getCurrency() {
		return currency;
	}
	public void setCurrency(String currency) {
		this.currency = currency;
	}
	public String getAccountState() {
		return accountState;
	}
	public void setAccountState(String accountState) {
		this.accountState = accountState;
	}
	public String getCreateOperatorId() {
		return createOperatorId;
	}
	public void setCreateOperatorId(String createOperatorId) {
		this.createOperatorId = createOperatorId;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public String getAccountType() {
		return accountType;
	}
	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}
	public String getAccountMac() {
		return accountMac;
	}
	public void setAccountMac(String accountMac) {
		this.accountMac = accountMac;
	}
	public Date getAccountModifyTime() {
		return accountModifyTime;
	}
	public void setAccountModifyTime(Date accountModifyTime) {
		this.accountModifyTime = accountModifyTime;
	}
	public int getVersion() {
		return version;
	}
	public void setVersion(int version) {
		this.version = version;
	} 
	public String getTraderName() {
		return traderName;
	}
	public void setTraderName(String traderName) {
		this.traderName = traderName;
	}
	public Account updateAccountBalance(BigDecimal money){
		//系统所有金额计算单位均为厘
		this.setLastBalance(this.getBalance()) ;									//记录上次余额
		//有符号的运算
		this.setAvailableMoney(this.getAvailableMoney().add(money)) ;		//可用余额增加
		this.setBalance(this.getBalance().add(money)) ;						//余额增加          
			
		this.setAccountModifyTime(new Date()) ;
		this.setLastModifyTime(new Date()) ;
		int mac = this.getBalance().intValue() 
				^ this.getFreezeMoney().intValue() ;
		char[] idArr = this.getAccountId().toCharArray() ;
		for(int i = 0 ; i < idArr.length ; i++){
			mac = mac ^ (int)idArr[i] ;
		}
		this.setAccountMac(String.valueOf(mac) ) ;
		this.setVersion(this.getVersion() + 1 ) ;
		this.setLastModifyTime(new Date()) ;
		return this ;
	}
	public boolean check(){
		String mac = this.getAccountMac() ;
		return mac.equals(mac()) ;
	}
	public String mac(){
		int mac = this.getBalance().intValue() 
				^ this.getFreezeMoney().intValue() ;
		char[] idArr = this.getAccountId().toCharArray() ;
		for(int i = 0 ; i < idArr.length ; i++){
			mac = mac ^ (int)idArr[i] ;
		}
		return String.valueOf(mac) ;
	}
	public Account saveAccount(Trader trader , Operator operator){
		BigDecimal zero = new BigDecimal("0") ;
		Date date = new Date() ;
		this.setTraderId(trader.getTraderId()) ;
		this.setAccountState("1") ;
		this.setAccountType("1") ;
		this.setAvailableMoney(zero) ;
		this.setBalance(zero) ;
		this.setCreateOperatorId(operator.getOperatorId()) ;
		this.setCreateTime(date) ;
		this.setCurrency("rmb") ;
		this.setFreezeMoney(zero) ;
		this.setLastBalance(zero) ;
		String password = MD5Util.MD5Encode(MD5Util.MD5Encode(this.getAccountId() + this.getPayPassword())).toLowerCase() ;
		this.setPayPassword(password) ;
		this.setVersion(0) ;
		this.setAccountMac(mac()) ;
		this.setAccountName(trader.getTraderName() + "保证金账户") ;
		return this ;
	}
}
